export class Mitt {
  all: Map<string, Function[]>;

  constructor() {
    this.all = new Map<string, Function[]>();
  }

  // 消息订阅
  on(type: string, handler: Function) {
    let handlers = this.all.get(type);
    if (!handlers) {
      this.all.set(type, (handlers = []));
    }

    handlers.push(handler);
  }

  // 消息发布
  emit(type: string) {
    let handlers = this.all.get(type);
    if (handlers) {
      handlers.map((handler) => {
        handler();
      });
    }
  }
}
